//System service for AVR

#include <Arduino.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include "sys.h"

void sys_reset()
{
  Serial.flush();
  //asm("CLI");
  //Serial.write("reset\n");
  
  //wdt_enable(WDTO_15MS);
  void (*reboot)(void);
  reboot = (void (*)(void))0x0000;
  (*reboot)();
  
  while(1)//等待溢出 
  {
    //Serial.println("waitrst");
    delay(1000);
  }
}

void sys_sleep()
{
  //Serial.println("sys:sleep");
  Serial.flush();
  
  sleep_enable();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_cpu();
  delayMicroseconds(2);
  
  //Serial.write("sys:wakeup\n");
  Serial.flush();
}

